/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PhysicCollection;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JPanel;

/**
 *
 * @author danh.ngothanh
 */
public class GravityPanel extends JPanel implements MouseListener, MouseMotionListener, Runnable{


//    private int  frame_count;
    private boolean isrunning;
    private ArrayList<Ripple2D> ripplelist;
    private double rippledelay;
    private int target_posx = 1;
    private int target_posy = 1;
    private int target_big_circle_width = 10;
    private int target_medium_circle_width = 7;
    private int target_small_circle_width = 4;
    private int score = 0;
    private int delay_frame = 0;
    private int max_delay_frame = 50;
    private Random random = new Random();
    public void GravityPanel(){
//        frame_count = 0;
    }

    public void init(){
        isrunning = true;
        ripplelist = new ArrayList<Ripple2D>();
        addMouseListener(this);
        addMouseMotionListener(this);
    }
    public void keyTyped(KeyEvent e) {

    }

    public void keyPressed(KeyEvent e) {

    }

    public void keyReleased(KeyEvent e) {

    }

    public void paint(Graphics g){
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.BLACK);
        g.drawString("Score == " + score, 5, 30);
        g.drawString("ripplelist.size == " + ripplelist.size(), 5, 15);
        g.setColor(Color.GREEN);
        for(int j = 0; j < ripplelist.size(); j ++){
            g.drawOval(ripplelist.get(j).getRippleStartPoint().x - ripplelist.get(j).getRippleWidth(), ripplelist.get(j).getRippleStartPoint().y - ripplelist.get(j).getRippleWidth(), ripplelist.get(j).getRippleWidth()*2, ripplelist.get(j).getRippleWidth()*2);
            g.drawOval(ripplelist.get(j).getRippleNorthPoint().x - ripplelist.get(j).getRippleWidth(), ripplelist.get(j).getRippleNorthPoint().y - ripplelist.get(j).getRippleWidth(), ripplelist.get(j).getRippleWidth()*2, ripplelist.get(j).getRippleWidth()*2);
            g.drawOval(ripplelist.get(j).getRippleEastPoint().x - ripplelist.get(j).getRippleWidth(), ripplelist.get(j).getRippleEastPoint().y - ripplelist.get(j).getRippleWidth(), ripplelist.get(j).getRippleWidth()*2, ripplelist.get(j).getRippleWidth()*2);
            g.drawOval(ripplelist.get(j).getRippleSouthPoint().x - ripplelist.get(j).getRippleWidth(), ripplelist.get(j).getRippleSouthPoint().y - ripplelist.get(j).getRippleWidth(), ripplelist.get(j).getRippleWidth()*2, ripplelist.get(j).getRippleWidth()*2);
            g.drawOval(ripplelist.get(j).getRippleWestPoint().x - ripplelist.get(j).getRippleWidth(), ripplelist.get(j).getRippleWestPoint().y - ripplelist.get(j).getRippleWidth(), ripplelist.get(j).getRippleWidth()*2, ripplelist.get(j).getRippleWidth()*2);
        }
        g.setColor(Color.BLACK);
        g.fillOval(target_posx - target_big_circle_width, target_posy - target_big_circle_width, target_big_circle_width*2, target_big_circle_width*2);
        g.setColor(Color.WHITE);
        g.fillOval(target_posx - target_medium_circle_width, target_posy - target_medium_circle_width, target_medium_circle_width*2, target_medium_circle_width*2);
        g.setColor(Color.BLACK);
        g.fillOval(target_posx - target_small_circle_width, target_posy - target_small_circle_width, target_small_circle_width*2, target_small_circle_width*2);
    }

    public void run() {
        while(isrunning){
            try{
                Thread.sleep(20);
            }catch(Exception e){}
//            frame_count++;
            if(!ripplelist.isEmpty()){
                for(int i = 0; i < ripplelist.size(); i ++){
                    ripplelist.get(i).update();
                }
            }
            for(int i = ripplelist.size() - 1; i >= 0; i --){
                if(ripplelist.get(i).isReachMaxSize()){
                    ripplelist.get(i).dest();
                    ripplelist.remove(i);
                }
            }
            if(delay_frame < max_delay_frame){
                delay_frame++;
            }else{
                delay_frame = 0;
                if(!(getWidth()<=0)){                    
                    target_posx = random.nextInt(getWidth() - target_big_circle_width*2) + target_big_circle_width;
                    target_posy = random.nextInt(getHeight() - target_big_circle_width*2) + target_big_circle_width;
                }
            }
            repaint();
        }
    }

    public void mouseClicked(MouseEvent e) {

    }

    public void mousePressed(MouseEvent e) {
        if(e.getButton() == MouseEvent.BUTTON3){
            for(int k = ripplelist.size() - 1; k >= 0; k --){
                ripplelist.get(k).dest();
                ripplelist.remove(k);
            }
        }
        else
            ripplelist.add(new Ripple2D(e.getX(), e.getY()));
    }

    public void mouseReleased(MouseEvent e) {
        
    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {
        
    }

    public void mouseDragged(MouseEvent e) {
//        if(System.currentTimeMillis() - rippledelay > 200){
//            ripplelist.add(new Ripple2D(e.getX(), e.getY()));
//            rippledelay = System.currentTimeMillis();
//        }
    }

    public void mouseMoved(MouseEvent e) {
        
    }

    public class Ripple2D{
        private int ripple_center_point_pos_x;
        private int ripple_center_point_pos_y;
        private int ripple_circle_width;
        private int ripple_north_point_pos_x;
        private int ripple_north_point_pos_y;        
        private int ripple_south_point_pos_x;
        private int ripple_south_point_pos_y;        
        private int ripple_east_point_pos_x;
        private int ripple_east_point_pos_y;        
        private int ripple_west_point_pos_x;
        private int ripple_west_point_pos_y;        

        private boolean is_reach_max_size;
        

        public Ripple2D(int pointx, int pointy){
            ripple_center_point_pos_x = pointx;
            ripple_center_point_pos_y = pointy;
            ripple_north_point_pos_x = ripple_center_point_pos_x;
            ripple_north_point_pos_y = -ripple_center_point_pos_y;
            ripple_south_point_pos_x = ripple_center_point_pos_x;
            ripple_south_point_pos_y = getHeight() + (getHeight() - ripple_center_point_pos_y);
            ripple_west_point_pos_x = getWidth() + (getWidth() - ripple_center_point_pos_x);
            ripple_west_point_pos_y = ripple_center_point_pos_y;
            ripple_east_point_pos_x = -ripple_center_point_pos_x;
            ripple_east_point_pos_y = ripple_center_point_pos_y;
            is_reach_max_size = false;
        }
        public void update(){
            if(!is_reach_max_size)
            ripple_circle_width += 3;
            if(ripple_circle_width > getWidth()*2)
                is_reach_max_size = true;
        }
        public Point getRippleStartPoint(){
            return new Point(ripple_center_point_pos_x, ripple_center_point_pos_y);
        }

        public Point getRippleNorthPoint(){
            return new Point(ripple_north_point_pos_x, ripple_north_point_pos_y);
        }

        public Point getRippleEastPoint(){
            return new Point(ripple_east_point_pos_x, ripple_east_point_pos_y);
        }

        public Point getRippleSouthPoint(){
            return new Point(ripple_south_point_pos_x, ripple_south_point_pos_y);
        }

        public Point getRippleWestPoint(){
            return new Point(ripple_west_point_pos_x, ripple_west_point_pos_y);
        }

        public int getRippleWidth(){
            return ripple_circle_width;
        }
        public boolean isReachMaxSize(){
            return is_reach_max_size;
        }

        public void dest(){
            ripple_center_point_pos_x = 0;
            ripple_center_point_pos_y = 0;
            ripple_circle_width = 0;
            ripple_north_point_pos_x = 0;
            ripple_north_point_pos_y = 0;
            ripple_south_point_pos_x = 0;
            ripple_south_point_pos_y = 0;
            ripple_east_point_pos_x = 0;
            ripple_east_point_pos_y = 0;
            ripple_west_point_pos_x = 0;
            ripple_west_point_pos_y = 0;
        }
    }
            
}
